$(function () {
    var dataQ;
	$('.way5').on('click',function(){	
   jzluxian()
    });
	
	function jzluxian() {
		var startTime = JSON.parse(localStorage.getItem("startTime"));
		var endTime = JSON.parse(localStorage.getItem("endTime"));
			console.log(startTime)
			console.log(endTime)
		$.ajax({
		    type: "GET",
		    url: baseUrl + "bzxlgl/main/guaranteelineviewcount/getlinename?startTime=" + startTime + '&endTime=' + endTime,
		    success: function (data) {
		        var dataJ = JSON.parse(data);
		        console.log(dataJ)
				$("#line-list")[0].innerHTML = '<option>全部</option>'
		        for (var i in dataJ) {
		            $("#line-list").append('<option value="' + dataJ[i].fid + '">' + dataJ[i].linename + '</option>');
		        }
		    }
		});
		$.ajax({
		    type: "GET",
		    url: baseUrl + "bzxlgl/main/guaranteelineviewcount/guaranteelinecount?startTime=" + startTime + '&endTime=' + endTime,
		    success: function (data) {
		        var dataJ = JSON.parse(data);
		        dataQ = dataJ;
		        $("#line_cont").html(dataJ.linesize);
		        $("#bh_cont").html(dataJ.damageasize);
		    }
		})
	}
    $('#line-list').change(function () {
        //清除聚合点
        clearCheckBox();
		var startTime = JSON.parse(localStorage.getItem("startTime"));
		var endTime = JSON.parse(localStorage.getItem("endTime"));
		console.log(startTime)
		console.log(endTime)
        var payment = $(this).children('option:selected').val();
		if(payment == '全部') {
			payment = ''
		}
			
        $('.way5').parent().find('.left-list ul>li>a').removeClass('curr');
        if (!payment) {
            $.ajax({
                type: "GET",
                url: baseUrl + "bzxlgl/main/guaranteelineviewcount/guaranteelinecount?startTime=" + startTime + '&endTime=' + endTime,
                success: function (data) {
                    var dataJ = JSON.parse(data);
                    dataQ = dataJ;
                    $("#line_cont").html(dataJ.linesize);
                    $("#bh_cont").html(dataJ.damageasize);
                    map.clearOverlays();
                    let layers = [];
                    if (dataQ.lineInfo instanceof Array) {
                        var list = dataQ.lineInfo;
                        $.each(list, function (index, item) {
                            var overlayears = JSON.parse(item.overlayears);
                            layers = layers.concat(overlayears);
                            commanDrwaing(overlayears, item.fid, 'pointLine', 'red');
                        })
                    } else {
                        var overlayears = JSON.parse(dataQ.lineInfo.overlayears);
                        layers = layers.concat(overlayears);
                        commanDrwaing(overlayears, dataQ.lineInfo.fid, 'pointLine', 'red');
                    }
                    setCenterAndZoom(layers);
                }
            });
        } else {
            $.ajax({
                type: "GET",
                url: baseUrl + "bzxlgl/main/guaranteelineviewcount/getlineanddamagebyid?fid=" + payment + "&startTime=" + startTime + '&endTime=' + endTime,
                success: function (data) {
                    var dataJ = JSON.parse(data);
                    dataQ = dataJ;
                    $("#line_cont").html(dataJ.lineCount);
                    $("#bh_cont").html(dataJ.damageSize);
                    map.clearOverlays();
                    let layers = [];
                    if (dataQ.lineInfo instanceof Array) {
                        var list = dataQ.lineInfo;
                        $.each(list, function (index, item) {
                            var overlayears = JSON.parse(item.overlayears);
                            layers = layers.concat(overlayears);
                            commanDrwaing(overlayears, item.fid, 'pointLine', 'red');
                        })
                    } else {
                        var overlayears = JSON.parse(dataQ.lineInfo.overlayears);
                        layers = layers.concat(overlayears);
                        commanDrwaing(overlayears, dataQ.lineInfo.fid, 'pointLine', 'red');
                    }
                    setCenterAndZoom(layers);
                }
            });
        }

    });

    $("#bzxl_button").on('click', function () {

        map.clearOverlays();
        //清除聚合点
        clearCheckBox();

        let layers = [];
        if (dataQ.lineInfo instanceof Array) {
            var list = dataQ.lineInfo;
            $.each(list, function (index, item) {
                var overlayears = JSON.parse(item.overlayears);
                layers = layers.concat(overlayears);
                commanDrwaing(overlayears, item.fid, 'pointLine', 'red');
            })
        } else {
            var overlayears = JSON.parse(dataQ.lineInfo.overlayears);
            layers = layers.concat(overlayears);
            commanDrwaing(overlayears, dataQ.lineInfo.fid, 'pointLine', 'red');
        }
        setCenterAndZoom(layers);
    });

    $("#bzxlbh_button").on('click', function () {
        var payment = $('#line-list').children('option:selected').val();
		if(payment == '全部') {
			payment = ''
		}
		console.log(payment)
        if (!payment) {
            map.clearOverlays();
            //清除聚合点
            clearCheckBox();

            var damageList = dataQ.damageList;
            commanDrwaing(damageList, 's-d.png', 'point', 'red');
            let layers = [];
            $.each(damageList, function (index, item) {
                layers = layers.concat(JSON.parse(item.overlayears));
                commanDrwaing(JSON.parse(item.overlayears), item.linefid, 'pointLine', 'red');
            });
            setCenterAndZoom(layers);
        } else {
            map.clearOverlays();
            //清除聚合点
            clearCheckBox();

            var damageList = dataQ.damageList;
            commanDrwaing(damageList, 's-d.png', 'point', 'red');
            commanDrwaing(JSON.parse(dataQ.lineInfo.overlayears), dataQ.lineInfo.fid, 'pointLine', 'red');
            damageList = damageList.concat(JSON.parse(dataQ.lineInfo.overlayears));
            setCenterAndZoom(damageList);
        }

    });

    /*
    data:地图加载数据为点的数组,
    img:打点图片,
    type:point 点,pointLine:线 */
    function commanDrwaing(data, img, type, setColor) {

        var pointarr = [];
        for (let n = 0; n < data.length; n++) {
            var point = new BMap.Point(data[n].lng, data[n].lat);

            point.data = data[n].fid;
            pointarr.push(point);
        }
        // map.centerAndZoom(pointarr[0], 15);
        if (pointarr.length > 0) {
            if (type == 'pointLine') {
                var options = $("#line-list option:selected");
                var polyline = new BMap.Polyline(pointarr, {
                    strokeColor: setColor,
                    strokeWeight: 5,
                    strokeOpacity: 1
                });
                polyline.id = img;
                polyline.addEventListener("click", function (e) {
                    console.log(e.target.id);
                    //线路病害详情
                    $.ajax({
                        type: "GET",
                        url: baseUrl + "bzxlgl/main/guaranteelineviewcount/getlineinfobyid?param=" + e.target.id,
                        success: function (data) {
                            openDiv(e, JSON.parse(data), "Line")
                        }
                    });
                });
                map.addOverlay(polyline); //将画出的线展示在地图上
            } else if (type == 'point') {
                for (var i = 0; i < pointarr.length; i++) {
                    var myIcon = new BMap.Icon("img/" + img + "", new BMap.Size(24, 24));
                    var pt = pointarr[i];
                    var marker = new BMap.Marker(pt, {
                        icon: myIcon
                    }); // 创建标注
                    marker.id = data[i].fid;
                    marker.addEventListener("click", function (e) {
                        //线路病害详情

                        $.ajax({
                            type: "GET",
                            url: baseUrl + "bzxlgl/main/guaranteelineviewcount/getlinedamageinfo?pk=" + e.target.id,
                            success: function (data) {
                                openDiv(e, JSON.parse(data), "point")
                            }
                        });
                    });
                    map.addOverlay(marker); // 将标注添加到地图中
                }
                // var options = {
                //     size: BMAP_POINT_SIZE_BIGGER,
                //     shape: BMAP_POINT_SHAPE_RHOMBUS,
                //     color: 'YELLOW'
                // }
                //     var pointCollection = new BMap.PointCollection(pointarr, options);  // 初始化PointCollection
                // pointCollection.addEventListener('click', function (e) {
                //     
                //     //alert('单击点的坐标为：' + e.point.lng + ',' + e.point.lat);  // 监听点击事件
                //     alert(e.point.data);
                // });
                // map.addOverlay(pointCollection);  // 添加Overlay

            }

        }
    }

    function openDiv(e, data, type) {
		 $('.wz-con'). remove();
        console.log(e)
        if (type === "point") {
            $('.disNCz').find('.yad-list') && $('.disNCz').find('.yad-list').remove();
            $('.disNCz').hide();
            $(e.currentTarget.Ac).find('.pointDisNCz') && $(e.currentTarget.Ac).find('.pointDisNCz').remove();
            $('.detailModel').append(
                // '<div class="pointDisNCz" style="position:absolute;padding:10px; left: -160px; width:688px; bottom:20px;z-index: 9999;background-color:#1D2127; border: 1px solid #0eb1fd;color:#fff; font-size:16px">'
                // + '<div class="yad-list">'
                // + '<i id="pointGbclod"><img src="img/icon.png" alt="" /></i>'
                '<table class="table" style="margin-bottom: 0;">'
                // + '<tr>'
                // + '<th>病害编码</th>'
                // + '<td>' + '</td>'
                // + '<th>类别</th>'
                // + '<td>' + expression(data,"damagetypeid")+ '</td>'
                + '</tr>'
                + '<tr>'
                + '<th>百度经度</th>'
                + '<td>' + expression(data, "bdlon") + '</td>'
                + '<th>百度纬度</th>'
                + '<td>' + expression(data, "bdlat") + '</td>'
                + '</tr>'
                + '<tr>'
                + '<th>位置</th>'
                + '<td>' + expression(data, "damageposision") + '</td>'
                + '<th>最迟完成时间</th>'
                + '<td>' + dataformat(expression(data, "lastdate")) + '</td>'
                + '</tr>'
                + '<tr>'
                + '<th>维修单位</th>'
                + '<td>' + expression(data, "keepOffice") + '</td>'
                + '<th>数量及单位</th>'
                + '<td>' + expression(data, "quangtization") + '</td>'
                + '</tr>'
                + '<tr>'
                + '<th>类别</th>'
                + '<td>' + expression(data, "damagetypeid") + '</td>'
                + '<th></th>'
                + '<td></td>'
                + '<tr>'
                + '<th>工作内容</th>'
                + '<td colspan="3">' + expression(data, "damagecontent") + '</td>'
                + '</tr>'
                + '<tr>'
                + '<th>采集照片</th>'
                + '<td colspan="3" height="125px">' +
                '<div id="imgDiv">' +
                '<img style="height: 125px;width: 120px" data-original="' + bzxlImageBaseUrl + (JSON.parse(data.beforePic))[0].storeFileName + '" src="' + bzxlImageBaseUrl + (JSON.parse(data.beforePic))[0].storeFileName + '" alt="">' +
                '<img style="height: 125px;width: 120px;margin-left: 25px" data-original="' + bzxlImageBaseUrl + (data.afterPic === undefined ? "" : (JSON.parse(data.afterPic))[0].storeFileName) + '"  src="' + bzxlImageBaseUrl + (data.afterPic === undefined ? "" : (JSON.parse(data.afterPic))[0].storeFileName) + '" alt="">' +
                '</div>' +
                '</td>'
                + '</tr>'
                + '</table>'
                // + '</div></div>'
            );
            $(e.currentTarget.Ac).find('#pointGbclod').click(function () {
                $(e.target.Ac).find('.pointDisNCz').remove();
            });
            $('.detailModel').show();
			document.querySelectorAll("img").forEach((img) => {
				console.log('tupian')
			    if(img.getAttribute("src") == bzxlImageBaseUrl || img.getAttribute("src") == null) {
			        img.style.display = "none";
			    }
			})
            viewer = new Viewer(document.getElementById('imgDiv'),{
                url: 'data-original',
                toolbar: false,
                button: true,

            })


        } else {
            $('.pointDisNCz') && $('.pointDisNCz').remove();
            $('.disNCz').find('.yad-list') && $('.disNCz').find('.yad-list').remove();
            var htmlStr =
                '<div class="yad-list lu"><i id="gbclod"><img src="img/icon.png" alt="" /></i>' +
                '<table class="table" style="margin-bottom: 0;">'
                + '<tr>'
                + '<th style="width:140px">保障线路</th>'
                + '<td>' + expression(data, "linename") + '</td>'
                + '</tr>'
                + '<tr>'
                + '<th style="width:140px">线路说明</th>'
                + '<td>' + expression(data, "remark") + '</td>'
                + '</tr>'
            '</table></div>'
            $('.disNCz').append(htmlStr);
            $('.disNCz').show();
            $('#gbclod').click(function () {
                $('.lu').remove();
                $('.disNCz').hide();
            })
        }
    }

    function dataformat(data) {
        var date = new Date(data)
        var Y = date.getFullYear() + '-'
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
        var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
        var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
        var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
        return Y + M + D + h + m + s
    }

    function expression(data, param) {

        return (data[param] === undefined ? "" : data[param]);
    }

    // $('.detailModel .model-close').click(function () {
    //     $('.detailModel').hide();
    //     $('.layui-layer-shade').hide();
    //     $('.detailModel table').remove();
    // });

   // pointDisNCz

});